1
|
|
|
const multisort = require('./modules/multisort.js'); |
2
|
|
|
const multifilter = require('./modules/multifilter.js'); |
3
|
|
|
const multikey = require('./modules/multikey.js'); |
4
|
|
|
const intersect = require('./modules/intersect.js'); |
5
|
|
|
const min = require('./modules/min.js'); |
6
|
|
|
const max = require('./modules/max.js'); |
7
|
|
|
const diff = require('./modules/diff.js'); |
8
|
|
|
const unique = require('./modules/unique.js'); |
9
|
|
|
const summ = require('./modules/summ.js'); |
10
|
|
|
const average = require('./modules/average.js'); |
11
|
|
|
const random = require('./modules/random.js'); |
12
|
|
|
const getByKey = require('./modules/getByKey.js'); |
13
|
|
|
const first = require('./modules/first.js'); |
14
|
|
|
const last = require('./modules/last.js'); |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Object helper |
18
|
|
|
*/ |
19
|
|
|
class Arr extends Array { |
20
|
|
|
static get [Symbol.species]() { |
21
|
|
|
return Array; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Sort a multiarray. |
26
|
|
|
* |
27
|
|
|
* @param {string} key |
28
|
|
|
* @param {string} direction |
29
|
|
|
* |
30
|
|
|
* @return {array} |
31
|
|
|
*/ |
32
|
|
|
multisort(key, direction) { |
33
|
|
|
return multisort(this[0], key, direction); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Filter a multi array. |
38
|
|
|
* |
39
|
|
|
* @param {string} key |
40
|
|
|
* @param {string} find |
41
|
|
|
* @param {boolean} operator |
42
|
|
|
* |
43
|
|
|
* @return {array} |
44
|
|
|
*/ |
45
|
|
|
multifilter(key, find, operator) { |
46
|
|
|
return multifilter(this[0], key, find, operator); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Only get some keys of a multi array. |
51
|
|
|
* |
52
|
|
|
* @param {string} key |
53
|
|
|
* |
54
|
|
|
* @return {array} |
55
|
|
|
*/ |
56
|
|
|
multikey(key) { |
57
|
|
|
return multikey(this[0], key); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get the intersection of arrays. |
62
|
|
|
* |
63
|
|
|
* @param {string} array |
64
|
|
|
* @param {boolean} multi |
65
|
|
|
* |
66
|
|
|
* @return {array} |
67
|
|
|
*/ |
68
|
|
|
intersect(array, multi) { |
69
|
|
|
return intersect(this[0], array, multi); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get the difference of arrays. |
74
|
|
|
* |
75
|
|
|
* @param {string} array |
76
|
|
|
* @param {boolean} total |
77
|
|
|
* |
78
|
|
|
* @return {array} |
79
|
|
|
*/ |
80
|
|
|
diff(array, total) { |
81
|
|
|
return diff(this[0], array, total); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the unique values of an array. |
86
|
|
|
* |
87
|
|
|
* @return {array} |
88
|
|
|
*/ |
89
|
|
|
get unique() { |
90
|
|
|
return unique(this[0]); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Only add the value if the value isnt in the array. |
95
|
|
|
* |
96
|
|
|
* @param {string} newValue |
97
|
|
|
* |
98
|
|
|
* @return {int} |
99
|
|
|
*/ |
100
|
|
|
pushIfNotExists(newValue) { |
101
|
|
|
if (this.indexOf(newValue) < 0) { |
102
|
|
|
this.push(newValue); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return this.length; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Add multiple values to an array. |
110
|
|
|
* |
111
|
|
|
* @param {array} newValues |
112
|
|
|
* |
113
|
|
|
* @return {int} |
114
|
|
|
*/ |
115
|
|
|
pushMultiple(newValues) { |
116
|
|
|
this.push(...newValues); |
117
|
|
|
|
118
|
|
|
return this.length; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Add multiple values to an array. |
123
|
|
|
* Only add the value if the value isnt in the array. |
124
|
|
|
* |
125
|
|
|
* @param {array} newValues |
126
|
|
|
* |
127
|
|
|
* @return {int} |
128
|
|
|
*/ |
129
|
|
|
pushMultipleIfNotExists(newValues) { |
130
|
|
|
const array = this; |
131
|
|
|
|
132
|
|
|
newValues.forEach((value) => { |
133
|
|
|
array.pushIfNotExists(value); |
134
|
|
|
}); |
135
|
|
|
|
136
|
|
|
return array.length; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* The largest of the given numbers. |
141
|
|
|
* If at least one of the arguments cannot be converted to a number, |
142
|
|
|
* NaN is returned. |
143
|
|
|
* |
144
|
|
|
* @return {int} |
145
|
|
|
*/ |
146
|
|
|
get max() { |
147
|
|
|
return max(this[0]); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* The smallest of the given numbers. |
152
|
|
|
* If at least one of the arguments cannot be converted to a number, |
153
|
|
|
* NaN is returned. |
154
|
|
|
* |
155
|
|
|
* @return {int} |
156
|
|
|
*/ |
157
|
|
|
get min() { |
158
|
|
|
return min(this[0]); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get a random value of an array. |
163
|
|
|
* |
164
|
|
|
* @return {string} |
165
|
|
|
*/ |
166
|
|
|
get random() { |
167
|
|
|
return random(this); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* The summ of all values. |
172
|
|
|
* |
173
|
|
|
* @return {int} |
174
|
|
|
*/ |
175
|
|
|
get summ() { |
176
|
|
|
return summ(this[0]); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get the average of all values. |
181
|
|
|
* |
182
|
|
|
* @return {int} |
183
|
|
|
*/ |
184
|
|
|
get average() { |
185
|
|
|
return average(this[0]); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Javascript implementation of Arr::get |
190
|
|
|
* |
191
|
|
|
* @param {string} key |
192
|
|
|
* @param {object|null} defaultValue |
193
|
|
|
* |
194
|
|
|
* @return {object|null} |
195
|
|
|
*/ |
196
|
|
|
getByKey(key, defaultValue) { |
197
|
|
|
return getByKey(this[0], key, defaultValue); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Javascript implementation of Arr::first |
202
|
|
|
* |
203
|
|
|
* @return {object|null} |
204
|
|
|
*/ |
205
|
|
|
get first() { |
206
|
|
|
return first(this[0]); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Javascript implementation of Arr::last |
211
|
|
|
* |
212
|
|
|
* @return {object|null} |
213
|
|
|
*/ |
214
|
|
|
get last() { |
215
|
|
|
return last(this[0]); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
module.exports = { |
220
|
|
|
Arr, |
221
|
|
|
multisort, |
222
|
|
|
multifilter, |
223
|
|
|
multikey, |
224
|
|
|
intersect, |
225
|
|
|
min, |
226
|
|
|
max, |
227
|
|
|
diff, |
228
|
|
|
unique, |
229
|
|
|
summ, |
230
|
|
|
average, |
231
|
|
|
random, |
232
|
|
|
getByKey, |
233
|
|
|
first, |
234
|
|
|
last, |
235
|
|
|
}; |
236
|
|
|
|